//
//  LeftPandectCustomTableViewCell.m
//  Delicious2
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "LeftPandectCustomTableViewCell.h"

@implementation LeftPandectCustomTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatSubviews];
    }
    return self;
}

- (void)setDetaileModel:(CookDetaileModel *)detaileModel {
    if (_detaileModel != detaileModel) {
        _detaileModel = detaileModel;
    }
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:detaileModel.thumb_image_url] placeholderImage:[UIImage imageNamed:@"ImagePlace"]];
    
    self.labelTitle.text = detaileModel.name;
    
    self.labelSubTitle.text = [NSString stringWithFormat:@"%@ 大卡/100克", detaileModel.calory];
    //关键字高亮
    NSString *str = self.labelSubTitle.text;
    NSRange range = [str rangeOfString:detaileModel.calory];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:str];
    [string addAttribute:NSForegroundColorAttributeName value:kColor range:NSMakeRange(range.location, range.length)];
    self.labelSubTitle.attributedText = string;
    
    //判断左侧button颜色
    if ([detaileModel.health_light.stringValue isEqualToString:@"1"]) {
        self.buttonRect.backgroundColor = [UIColor greenColor];
    } else if ([detaileModel.health_light.stringValue isEqualToString:@"2"]) {
        self.buttonRect.backgroundColor = [UIColor yellowColor];
    } else {
        self.buttonRect.backgroundColor = [UIColor redColor];
    }
}

- (void)creatSubviews {
    self.imageV = [[UIImageView alloc] initWithFrame:CGRectZero];
    self.imageV.layer.masksToBounds = YES;
    self.imageV.layer.cornerRadius = 10;
    
    self.labelTitle = [[UILabel alloc] initWithFrame:CGRectZero];
    self.labelTitle.font = [UIFont boldSystemFontOfSize:18];
    
    self.labelSubTitle = [[UILabel alloc] initWithFrame:CGRectZero];
    
    self.buttonRect = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonRect.layer.cornerRadius = 10;
    
    
    [self addSubview:self.imageV];
    [self addSubview:self.labelTitle];
    [self addSubview:self.labelSubTitle];
    [self addSubview:self.buttonRect];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageV.frame = CGRectMake(10, 10, 80, 80);
    self.labelTitle.frame = CGRectMake(self.imageV.frame.size.width + 20, 10, self.frame.size.width - 150, 40);
    self.labelSubTitle.frame = CGRectMake(self.labelTitle.frame.origin.x, 50, self.labelTitle.frame.size.width, 40);
    self.buttonRect.frame = CGRectMake(self.frame.size.width - 30, self.frame.size.height / 2 - 10, 20, 20);
    [self.imageV nightMode];
}

@end
